#include "stdafx.h"
#include "MainWindow.h"
#include <Downloader.cpp>
// 例如在MainWindow的某个函数中启动下载
void MainWindow::on_downloadButton_clicked() {
    QString url = ui->urlLineEdit->text();
    QString savePath = ui->pathLineEdit->text();

    Downloader* downloader = new Downloader(this);
    connect(downloader, &Downloader::progressChanged, this, &MainWindow::updateProgress);
    connect(downloader, &Downloader::downloadFinished, this, &MainWindow::handleDownloadFinished);

    downloader->startDownload(QUrl(url), savePath);
}

void MainWindow::updateProgress(qint64 bytesReceived, qint64 bytesTotal) {
    if (bytesTotal > 0) {
        int percent = static_cast<int>((bytesReceived * 100) / bytesTotal);
        ui->progressBar->setValue(percent);
    }
}

void MainWindow::handleDownloadFinished(bool success) {
    if (success) {
        QMessageBox::information(this, "成功", "文件下载完成！");
    }
    else {
        QMessageBox::warning(this, "失败", "文件下载失败！");
    }
    // 进行一些清理或状态重置工作
}